package org.wizehack.mri.testSet;

import java.util.ArrayList;
import java.util.List;

public class Regions {
	public List<SrcFile> fileList;
	private static Regions regions = null;
	
	private Regions(){
		
	}
	
	public static synchronized Regions getInstance(){
		if(regions == null){
			regions = new Regions();
			regions.fileList = new ArrayList<SrcFile>();
		}
		
		return regions;
	}
	
	public SrcFile getNewSrcFileInstance(){
		return new SrcFile();
	}
	
	public Code getCodeNewInstance(){
		return new Code();
	}
	
	public Regions getFailedOnlyRegion(){
		Regions failedOnlyRegion = new Regions();
		List<SrcFile> failedOnlyRegionfileList = new ArrayList<SrcFile>();

		Regions regions = Regions.getInstance();
		for(int i=0; i<regions.fileList.size(); i++){
			SrcFile srcFile = regions.fileList.get(i);
			
			SrcFile failedOnlySrcFile = new SrcFile();
			failedOnlySrcFile.fileName = srcFile.fileName;
			
			for(int j=0; j<srcFile.codeList.size(); j++){
				Code code = srcFile.codeList.get(j);
				
				if(code.getCoveredCount() == 1){
					failedOnlySrcFile.codeList.add(code);
				}
			}//end for j
			
			if(failedOnlySrcFile.codeList.size() > 0){
				failedOnlyRegionfileList.add(failedOnlySrcFile);
			}
		}//end for i

		if(failedOnlyRegionfileList.size() > 0){			
			for(int i=0; i<failedOnlyRegionfileList.size(); i++){
				SrcFile srcFile = failedOnlyRegionfileList.get(i);
				failedOnlyRegion.fileList.add(srcFile);
			}			
		}
		
		return failedOnlyRegion;
	}
	
	public Regions getMixedRegion(int numberOfTestCase){
		Regions regions = Regions.getInstance();
		Regions mixedRegion = new Regions();
		List<SrcFile> mixedRegionfileList = new ArrayList<SrcFile>();
		mixedRegion.fileList = new ArrayList<SrcFile>();
		for(int i=0; i<regions.fileList.size(); i++){
			SrcFile srcFile = regions.fileList.get(i);
						
			SrcFile mixedSrcFile = new SrcFile();
			mixedSrcFile.fileName = srcFile.fileName;
			for(int j=0; j<srcFile.codeList.size(); j++){
				Code code = srcFile.codeList.get(j);
				
				if(code.getCoveredCount() < numberOfTestCase && code.getCoveredCount() > 0){
					mixedSrcFile.codeList.add(code);
				}
			}//end for j
			
			if(mixedSrcFile.codeList.size() > 0){
				mixedRegionfileList.add(mixedSrcFile);
			}
		}//end for i
		
		if(mixedRegionfileList.size() > 0){			
			for(int i=0; i<mixedRegionfileList.size(); i++){
				SrcFile srcFile = mixedRegionfileList.get(i);
				mixedRegion.fileList.add(srcFile);
			}			
		}
		
		return mixedRegion;
	}
	
	class SrcFile {
		String fileName;
		List<Code> codeList = new ArrayList<Code>();
	}
	
	class Code{
		int number;
		int coveredCount=0;
		
		public void increaseCount(){
			coveredCount++;
		}
		
		public int getCoveredCount(){
			return this.coveredCount;
		}
	}
}
